import { ExtractJwt, Strategy } from 'passport-jwt';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { AuthService } from './auth.service';

@Injectable()
export class jwtStrategy extends PassportStrategy(Strategy) {
  constructor(private readonly authService: AuthService) {
    super({
        jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
        ignoreExpiration: false,
        secretOrKey: 'jwtConstantsSecret',
      });
  }
  async validate(payload: any) {
    // req.user
    // console.log(payload);
    
    return { userId: payload.sub, username: payload.username };
  }
//   以下是local策略，需要重写
//   async validate(name: string, passsword: string): Promise<any> {
//     console.log("username:"+name+"pass:"+passsword  );
        

//     const user = await this.authService.validateUser(name, passsword);
//     if (!user) {
//       throw new UnauthorizedException();
//     }
//     return user;
//   }
}
